home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.direct.ca!usenet
- From: qjackson@direct.ca
- Newsgroups: comp.lang.c++
- Subject: Re: Syntax clarification
- Date: Sun, 18 Feb 1996 16:14:58 GMT
- Organization: Parsepolis Software
- Message-ID: <4g7ja4$spm@aphex.direct.ca>
- References: <31236966.1881@sisna.com>
- Reply-To: qjackson@direct.ca
- NNTP-Posting-Host: 204.174.245.64
- X-Newsreader: Forte Free Agent 1.0.82
-
- Matt Smolic <msmolic@sisna.com> wrote:
-
- >I am just making the switch from C to C++. Something I have come across
- >and cannot find an answer to is the use of the "&" symbol. In C this
- >means the address of, (e.g.) scanf("%d", &Avariable) would return the
- >address of Avariable. In C++ I constantly see syntax such as, in
- >declaring a class, Complex pow(const Complex & c, const Complex & power)
- >What does the & symbol do here?
-
- It means that c and power are passed by "reference" -- rather than by
- copy. (That is to say, "c" and "power" are aliases that point to the
- actual data, not to a copy of the data.) There is no need within the
- function pow to dereference a pointer, as there is with the *pointer
- syntax.
-
- Note, however, that the "const" modifier means that, although the
- address is passed, the variable may not be modified within the scope
- of pow. This is a method of passing variables quickly, and having the
- compiler make certain that the value is not changed within the
- function. (It prevents a copy being made, which, with large objects,
- involves a lot of time and is sometimes semantically deadly.)
-
- So:
-
- int foo (const int& a, int* b, int c)
- {
-
- return a + *b + c;
-
- }:
-
-
- "Pass a by reference, but don't let anyone change its value within the
- function via the alias, pass b as a pointer, requiring explicit
- dereferencing within the function to get its value, and pass c as a
- copy."
-
- Hope this explains things.
-
-
- Cheers,
-
-
- --
- |
- Parsepolis Software | Quinn Tyler Jackson
- "ParseCity" | (aka 'Jamshid')
- >--------------------------| qjackson@direct.ca
- |---------------------->
-
-